#region Imported Namespaces
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

using PHSRAG.Insight21.BusinessObjects;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
using PHSRAG.Utility;
#endregion

namespace PHSRAG.Insight21.CDS
{
	/// <summary>
	/// Summary description for RefCode.
	/// </summary>
	public class RefCode : SmartBusinessObject
	{
		#region Constants
		private const string RefCodeKey = "refCodeKey_33E9F9CD-9D2D-4925-9F55-1E36FBFF2D56";
		#endregion

		#region Instance Variables
		private string domain;
		private string code;
		private string meaning;
		private bool active;
		private Hashtable attributes;
		#endregion Instance Variables
		
		#region Constructors
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="cachePolicy">Prevalent CachePolicy</param>
		/// <param name="domain">RefDomain</param>
		/// <param name="code">RefCode</param>
		/// <param name="meaning">RefMeaning</param>
		/// <param name="active">Active/Inactive flag</param>
		/// <param name="attributes">Attributes collection</param>
		public RefCode(ICachePolicy cachePolicy, string domain, string code, string meaning, char active, Hashtable attributes) : base(cachePolicy)
		{
			this.domain = domain;
			this.code = code;
			this.meaning = meaning;
			this.active = (active == 'Y') ? true : false;
			this.attributes = attributes;
			dirty = true;
		}

		
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="cachePolicy">Prevalent cachepolicy</param>
		/// <param name="dataRow">DataRow holding the values to be assigned to instance variables</param>
		public RefCode(ICachePolicy cachePolicy, DataRow dataRow) : base(cachePolicy)
		{
			PopulateDataMembers(dataRow);
		}
		#endregion Constructors

		#region Properties
		public string Domain
		{
			get { return domain; }
		}

		public string Code
		{
			get { return code; }
		}

		public string Meaning
		{
			get { return meaning; }
		}

		/// <summary>
		/// The value of the RefCode database table column Active is assumed to be Y/N. 
		/// This is stored as a true/false value inside the object
		/// </summary>
		public char Active
		{
			get { return (active == true) ? 'Y' : 'N'; }
		}

		public Hashtable Attributes
		{
			get { return attributes; }
		}
		/// <summary>
		/// Indexer to the RefCode object. Takes attributeName as an input and returns the attributeValue. 
		/// If the attribute with the specified name does not exist then the indexer returns an empty string.
		/// </summary>
		public string this[string attributeName]
		{
			get
			{
            object attribute = attributes[attributeName];
            return ( null == attribute ? string.Empty : attribute.ToString() );
			}
		}
		#endregion Properties

		#region SBO Overrides
		/// <summary>
		/// Saves the RefCode object's data to the data repository and sets the state back to non-dirty
		/// </summary>
		/// <param name="sqlConnection">Sql Connection object</param>
		protected override void SaveObject(SqlTransaction transaction)
		{
			try
			{
				DataAccess.ExecuteNonQuery(
					transaction.Connection,
					transaction,
					"SaveRefCode", 
					DataAccess.BuildSqlParameterArray(
					"@refDomain", SqlDbType.VarChar, domain,
					"@refCode", SqlDbType.VarChar, code,
					"@refMeaning", SqlDbType.VarChar, meaning,
					"@active", SqlDbType.Char, (active) ? 'Y' : 'N'));

				dirty = false;
			}
			catch(Exception e)
			{
				throw new Exception("Save of RefCode failed", e);
			}
		}
		
		/// <summary>
		/// Delete the Person object. DeleteObject is not currently implemented, 
		/// and will throw an exception if called. 
		/// </summary>
		/// <param name="sqlTransaction">transaction to use if were able to delete.</param>
		protected override void DeleteObject( SqlTransaction sqlTransaction)
		{
			throw new Exception("RefCode.DeleteObject is not supported");
		}

		/// <summary>
		/// Load the current instance with person data
		/// </summary>
		/// <param name="connectionString">contains info to connect to the data source</param>
		/// <param name="args">arguments for the stored procedure</param>
		public override void Load(string connectionString, params object[] args)
		{
			throw new Exception("RefCode.Load is not supported");
//			if (args.Length != 1)
//				throw new Exception("RefCode load requires a code");
//
//			try
//			{
//				PopulateDataMembers(DataAccess.GetDataSet(
//					connectionString,
//					"GetRefCode",
//					DataAccess.BuildSqlParameterArray("@refCode", SqlDbType.VarChar, args[0])).Tables[0].Rows[0]);
//
//				Id = -1;
//
//			}
//			catch(Exception e)
//			{
//				throw new Exception("RefCode load failed", e);
//			}
		}
        /// <summary>
        /// This method will load the Last feed run dates to be displayed in the tracking pages.
        /// </summary>
        public static DataTable GetLastFeedDates()
        {
            //The following block will load the dates for the Latest feeds that were run for various modules in application cache.
            DataTable dTable = null;
            dTable = DataAccess.GetDataTable(DataAccess.GetConnectionString()
                    , "GetLastFeedRunDates", null);
            DataColumn[] dtColumns = new DataColumn[1];
            dtColumns[0] = dTable.Columns["FeedrunType"];
            dTable.PrimaryKey = dtColumns;
           //if (dTable != null && dTable.Rows.Count > 0)
               return dTable;
        }


		/// <summary>
		/// Cache this instance using the prevalent cache policy.
		/// </summary>
		public override void Cache()
		{
			CachePolicy.Cache(RefCodeKey, this);
		}

		/// <summary>
		/// Remove this instance from the cache using the prevalent cache policy.
		/// </summary>
		public override void UnCache()
		{
			CachePolicy.UnCache(RefCodeKey);
		}


		/// <summary>
		/// Cache Key uniquely identifies this object within the cache.
		/// </summary>
		/// <returns>the unique string for this object.</returns>
		public override string CacheKey()
		{
			return RefCodeKey;
		}
		#endregion
		
		#region Private Methods
		/// <summary>
		/// Populates instance variables with values from the datarow
		/// </summary>
		/// <param name="dataRow">DataRow holding the values to be assigned to instance variables</param>
		private void PopulateDataMembers(DataRow dataRow)
		{
			domain = (string)dataRow["RefDomain"];
			code = (string)dataRow["RefCode"];
			meaning = (string)dataRow["RefMeaning"];
			active = (dataRow["Active"].ToString()[0] == 'Y') ? true : false;
		}
		#endregion
	}
}

